let http = require('http');
let path = require('path');
let crypto = require('crypto');


let server = http.createServer(function (req, res) {
  res.writeHead(200, {
    'Transfer-Encoding': 'chunked', //参看HTTP 协议中的 Transfer-Encoding.md
    'Trailer': 'Content-MD5'
  });
  let rs = require('fs').createReadStream(path.join(__dirname, 'msg.txt'), {
    highWaterMark: 2
  });
  let md5 = crypto.createHash('md5');
  rs.on('data', function (data) {
    console.log(data);
    res.write(data); //write几次 对面on data就会触发几次 是一一对应的(前提是设置了 'Transfer-Encoding': 'chunked')
    md5.update(data);
  });
  rs.on('end', function () {
    res.addTrailers({
      'Content-MD5': md5.digest('hex')
    });
    res.end();
  });
}).listen(8080);
